package com.example.rk.controller;

import com.example.rk.common.MyPageInfo;
import com.example.rk.entity.RenKo;
import com.example.rk.service.RenKoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/renko")
public class RenKoController  {
    @Autowired
    private RenKoService service;
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id)
    {
        service.deleteById(id);
        return "redirect:/admin/main";}
    @RequestMapping("/doAdd")
    @ResponseBody
    public String doAdd(RenKo renko){
        return service.doAdd(renko);
    }

    @RequestMapping("/openEdit/{id}")
    public ModelAndView openEdit(@PathVariable("id") int id, ModelAndView mv){
        RenKo renko=service.findById(id);
        mv.setViewName("/renko/edit");
        mv.addObject("renko",renko);
        return mv;
    }
    @RequestMapping("/list")
    public ModelAndView main(HttpServletRequest request, ModelAndView mv){
        mv.setViewName("/renko/list");
        //String data=request.getParameter("data");
        //String name=request.getParameter("name");
        MyPageInfo myPageInfo= service.findBySearch(request);
        //List<RenKo> courseList= renKoService.findAll();
        mv.addObject("searchdata",request.getParameter("data"));
       mv.addObject("searchdiqu",request.getParameter("diqu"));
        mv.addObject("pageInfo",myPageInfo);
        return mv;
    }

    @RequestMapping("/doEdit")
    @ResponseBody
    public String doEdit(RenKo renko){
        return service.doEdit(renko);
    }
}

